package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.ShopEntityClassify;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.views.ExpandGridView;

public class ServerShopExpandListAdapter extends BaseExpandableListAdapter{
    private final Context mContext;
    private final  LayoutInflater mInflater;
    final List<ShopEntityClassify> mGroup;
    final List<List<ShopEntityClassify>> mChild;
    private final List<ExpandGridListAdapter> mGridListAdapters;
	
    public ServerShopExpandListAdapter(Context ctx, List<ShopEntityClassify> group,List<List<ShopEntityClassify>> child) {
        mContext = ctx;
        mGroup = group;
        mChild = child;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGridListAdapters = new ArrayList<ServerShopExpandListAdapter.ExpandGridListAdapter>();
        for (List<ShopEntityClassify> list : child) {
        	mGridListAdapters.add(new ExpandGridListAdapter(list, mInflater));
		}
    }

    @Override
    public int getGroupCount() {
        return mGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
    	return mChild.get(groupPosition).size();
    }

    @Override
    public ShopEntityClassify getGroup(int groupPosition) {
        return mGroup.get(groupPosition);
    }

    @Override
    public ShopEntityClassify getChild(int groupPosition, int childPosition) {
    	return mChild.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
    	 GroupViewHolder groupViewHolder;
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.location_group_item_layout, null);
            groupViewHolder= new GroupViewHolder();
            groupViewHolder.mGroupName = (TextView)convertView.findViewById(R.id.group_name);
            groupViewHolder.mGroupPic=(ImageView)convertView.findViewById(R.id.group_pic);
            convertView.setTag(groupViewHolder);
        }else {
        	groupViewHolder = (GroupViewHolder) convertView.getTag();
		}
        if(isExpanded){
        	groupViewHolder.mGroupPic.setBackgroundResource(R.drawable.arrows_down_city);
        }else{
        	groupViewHolder.mGroupPic.setBackgroundResource(R.drawable.arrows);
        }
        groupViewHolder.mGroupName.setText(getGroup(groupPosition).getCLASS_NAME());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
    	ExpandGridView mExpandGridView;
        if(convertView == null){
        	mExpandGridView = new ExpandGridView(mContext);
        	mExpandGridView.setNumColumns(4);
        	convertView = mExpandGridView;
        	if(mContext instanceof AdapterView.OnItemClickListener){
        		mExpandGridView.setOnItemClickListener((AdapterView.OnItemClickListener)mContext);
        	}
        }else{
        	mExpandGridView = (ExpandGridView)convertView;
        }
        ExpandGridListAdapter gridListAdapter = mGridListAdapters.get(groupPosition);
        mExpandGridView.setAdapter(gridListAdapter);
    	return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        /*很重要：实现ChildView点击事件，必须返回true*/
        return true;
    }

    private static class GroupViewHolder {
        TextView mGroupName;
        ImageView mGroupPic;
    }

	public static class ExpandGridListAdapter extends BaseAdapter{
		final List<ShopEntityClassify> mList;
		final LayoutInflater mInflater;
		public ExpandGridListAdapter(List<ShopEntityClassify> list,LayoutInflater inflater) {
			this.mList = list;
			this.mInflater = inflater;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public ShopEntityClassify getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ShopEntityClassify entity = getItem(position);
			TextView textView = null;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.server_shop_list_item,null);
				textView = (TextView)convertView.findViewById(R.id.text_item);
				convertView.setTag(textView);
			}else{
				textView = (TextView)convertView.getTag();
			}
			textView.setText(entity.getCLASS_NAME());
			textView.setTextSize(14);
			return convertView;
		}
	}
	
}
